<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2008 Starbugs <univis2typo3@googlegroups.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib.'class.tslib_pibase.php');


/**
 * Helper 'person' for the 'univis2typo3' extension.
 *
 * This helper class is here to provide functions related to person-specific data.
 * E.g. Name, Title, Contact data
 *
 * @author	Starbugs <univis2typo3@googlegroups.com>
 * @package	TYPO3
 * @subpackage	tx_univis2typo3
 */
class tx_univis2typo3_person extends tslib_pibase {
	var $prefixId      = 'tx_univis2typo3_person';		// Same as class name
	var $scriptRelPath = 'common/class.tx_univis2typo3_person.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'univis2typo3';	// The extension key.
	//var $pi_checkCHash = true;
	//var $cObj = "";

	var $language = 0;
	var $user_id  = 0;
	var $dept_id  = 0;

	/** C'tor
	 *
	 */
	function tx_univis2typo3_person(&$conf) {
		$this->language = $GLOBALS["TSFE"]->sys_language_uid;
		$this->user_id  = (int) $GLOBALS['TSFE']->page['perms_userid'];
		$this->dept_id  = (int) $conf['department_id'];
	}

	/** Load all (most?) data for the current person.
	 *
	 * This data is used to display the personal-page header
	 */
	function loadPersonData() {
		if (!$this->user_id) return false;

		// find univis key of page owner
		$select  = '`b`.`tx_univis2typo3_person_key`, `b`.`tx_univis2typo3_image`'
			. ', `txp`.`firstname`, `txp`.`lastname`, `txp`.`title`, `txp`.`atitle`'
			. ', `upl`.`office`'
			. ', `trg`.`description` AS `job_desc_de`, `trg`.`description_en` AS `job_desc_en`'
			. ', `trg`.`job_url` AS `job_url_de`, `trg`.`job_url_en` AS `job_url_en`'
			. ', `po`.`starttime` AS `office_start`, `po`.`endtime` AS `office_end`, `po`.`office` AS `office_room`'
			. ', `po`.`repeat` AS `office_repeat`, `po`.`comment` AS `office_comment`'
			;
		$from    = '`be_users` AS `b`'
		       	. ' LEFT JOIN `tx_u2t3_person` AS `txp` ON `txp`.`key` = `b`.`tx_univis2typo3_person_key`'
			. ' LEFT JOIN `u_person_location` AS `upl` ON `upl`.`person_key` = `b`.`tx_univis2typo3_person_key`'
		       	. ' LEFT JOIN `u_person_jobs` AS `upj` ON `upj`.`per` = `b`.`tx_univis2typo3_person_key`'
				. ' LEFT JOIN `tx_u2t3_job` AS `trg` ON `trg`.`description` = `upj`.`job_key`'
		       	. ' LEFT JOIN `u_person_officehour` AS `po` ON `po`.`person_key` = `b`.`tx_univis2typo3_person_key`'
			;
		$where   = '`b`.`uid` = ' . $this->user_id;
		$groupby = $limit = '';
		$orderby = '';
		$query = $GLOBALS['TYPO3_DB']->SELECTquery(
			$select,
			$from,
			$where,
			$groupby,
			$orderby,
			$limit
		);
		
		$res =& $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);
		$row =& mysql_fetch_assoc($res);

		$pData = new stdClass();
		// $btitle     = ''; // title, before name (e.g. Dipl.-Inf.)
		// $atitle     = ''; // title, after name (e.g. M.Sc.)
		// $group = ''; // e.g. Med. Img. Seg., etc.
		// $function = ''; // e.g. Researcher, Lead, ...
		$pData->personKey  = $row['tx_univis2typo3_person_key'];
		$pData->firstName = trim($row['firstname']);
		$pData->lastName  = trim($row['lastname']);
		$pData->imgUrl = $row['tx_univis2typo3_image'];
		$pData->btitle = $row['title'];
		$pData->atitle = $row['atitle'];
		$pData->group  = (1==$this->language) ? $row['job_desc_de'] : $row['job_desc_en'];
		$pData->groupUrl = (1==$this->language) ? $row['job_url_de'] : $row['job_url_en'];
		$pData->personName = trim($pData->firstName . ' ' . $pData->lastName);
		if ('/' == $pData->imgUrl{0}) {
		    $pData->imgUrl = substr($pData->imgUrl, 1);
		}
		$pData->office = $row['office'];
		$pData->officeHours = array();
		if($row['office_start'] != "") 	$pData->officeHours['start'] = $row['office_start'];
		if($row['office_end'] != "") 	$pData->officeHours['end']   = $row['office_end'];
		if($row['office_room'] != "") 	$pData->officeHours['room']  = $row['office_room'];
		if($row['office_repeat'] != "") {
			if(1==$this->language)
				$day = array("Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag");
			else
				$day = array("Sunday", "Monday", "Tuesday", "Wednesday","Thursday","Friday","Saturday");
			$r = explode(" ", $row['office_repeat']);
//			TODO: $r[0] contains w1 (I guess for weekly) and d1 (for daily)
			$pData->officeHours['day'] = (count($r) == 2) ? $day[ $r[1] ] : "";			
		}
		if($row['office_comment'] != "") $pData->officeHours['comment'] = $row['office_comment'];

		return $pData;
	}

	/** Load contact data for the current person.
	 *
	 * This data will be displayed in the contact box on the left.
	 * If no person key is given, load generic info
	 *
	 */
	function loadContactData()
	{
		if (!$this->user_id) {
			return $this->loadOrgContactData();
		}

		$select  = '`b`.`tx_univis2typo3_person_key`'
			. ', `txp`.`firstname`, `txp`.`lastname`'
			. ', `upl`.`email`, `upl`.`tel`, `upl`.`fax`, `upl`.`office`, `upl`.`street`, `upl`.`ort`'
			;
		$from    = '`be_users` AS `b`'
			. ' LEFT JOIN `tx_u2t3_person` AS `txp` ON `txp`.`key` = `b`.`tx_univis2typo3_person_key`'
			. ' LEFT JOIN `u_person_location` AS `upl` ON `upl`.`person_key` = `b`.`tx_univis2typo3_person_key`'
			;
		$where   = '`b`.`uid` = ' . $this->user_id;
		$groupby = $orderby = $limit = '';
		$query = $GLOBALS['TYPO3_DB']->SELECTquery(
			$select,
			$from,
			$where,
			$groupby,
			$orderby,
			$limit
		);

		$res =& $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);
		if (mysql_num_rows($res) != 1) {
			// user not found / or not associated with UnivIS data
			//print 'user not found [1]';
			return $this->loadOrgContactData();
		}
		$contactData = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
		if (!$contactData['tx_univis2typo3_person_key']) {
			// user not associated with UnivIS data
			//print 'user not found [2]';
			return $this->loadOrgContactData();
		}

		$contactData['persName'] = trim($contactData['firstname'] . ' ' . $contactData['lastname']);
		return $contactData;
	}

	function loadOrgContactData() {
		$query = "SELECT * FROM `tx_u2t3_org` WHERE `department_id` = {$this->dept_id}";
		$res = $GLOBALS['TYPO3_DB']->sql_query($query);
		$contactData = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);

		return $contactData;

		/*
		$content = '<div class="address">'
			. $lehrstuhlname . '<br />'
			. $row['street'] . '<br />'
			. $row['ort'] . '<br />'
			. "</div>\n"
			. "<div class=\"email\">{$row['email']}</div>\n"
			. "<div class=\"phone\">{$row['tel']}</div>\n"
			. "<div class=\"fax\">{$row['fax']}</div>\n";
		*/
	}
}
?>
